<?php
namespace Api\Controller;
class AccountController extends CommonController{
	//账户信息
	public function account_info(){
		$user_id = $this->user_id();

		$data['usable_money'] = D('Account')->get_account_money($user_id, 'usable_account');
		$data['used_money']   = D('Account')->get_account_money($user_id, 'used_account');

		$this->success('获取数据成功！', $data);
	}
	//账单记录
	public function bill_list(){
		$user_id = $this->user_id();
		//分页参数
		$page      = $this->_get('page', '', 1);
		$pagesize  = $this->_get('pagesize', '', 10);
		$first_row = $pagesize * ($page - 1);

		$where['user_id'] = $user_id;

		$account_type = $this->_get('account_type');
		if($account_type){
			$where['account_type'] = $account_type;
		}

		$order['bill_id'] = 'desc';

		$count = D('UserBill')->where($where)->count();
		$list  = D('UserBill')->where($where)->limit($first_row . ',' . $pagesize)->order($order)->select();
		foreach ($list as $rs) {
			$new_list[] = D('Account')->filter_field($rs);
		}

		$data['list']      = $new_list ? $new_list : array();
		$data['page_info'] = $this->page_info($count, $pagesize);


		if($new_list){
			$this->success('获取数据成功！', $data);
		}else{
			$this->success('找不到相关数据!', $data);
		}
	}

	//添加银行卡
	public function add_bank_card(){
		$data['user_id']        = $this->user_id();
		$data['card_type']      = $this->_get('card_type','请选择所属银行');
		$data['city_id']        = $this->_get('city_id');
		$data['open_bank_name'] = $this->_get('open_bank_name');
		$data['account_name']   = $this->_get('account_name','请填写账号名称');
		$data['card_no']        = $this->_get('card_no','请填写账号');
		$data['create_time']    = time();

		//判断是否存在此银行
		if(D('Category')->is_exists_id($data['card_type']) == false){
			$this->error('请选择所属银行！');
		}

		$where['user_id'] = $data['user_id'];
		$where['card_no'] = $data['card_no'];
		$where['status']  = 1;

		if(D('UserBankCard')->where($where)->count()){
			$this->error('此银行卡，您已添加过了！');
		}
		//添加银行卡记录
		D('UserBankCard')->add($data);
		//返回最后插入ID
		$data['card_id']        = D('UserBankCard')->getLastInsID();
		$data['card_ico']       = D('Category')->get_ico($data['card_type']);
		$data['card_type_name'] = D('Category')->get_name($data['card_type']);

		$this->success('添加成功！',$data);
	}
	//解除绑定银行卡
	public function unbind_bank_card(){
		$where['user_id'] = $this->user_id();
		$where['card_id'] = $this->_get('card_id','请选择银行卡号');
		if(!D('UserBankCard')->where($where)->count()){
			$this->error('解除绑定失败->银行卡ID['.$where['card_id'].']不存在！',$data);
		}

		D('UserBankCard')->where($where)->save(array('status'=>0));
		$this->success('解除绑定成功！',$data);
	}
	//我的银行卡
	public function my_bank_card(){
		$user_id = $this->user_id();
		$where['user_id'] = $user_id;
		$where['status']  = 1;
		$list = D('UserBankCard')->where($where)->select();
		foreach ($list as $rs) {
			$rs['card_type_name'] = D('Category')->get_name($rs['card_type']);
			$rs['city_name']      = D('Area')->id2names($rs['city_id']);
			$rs['card_ico']       = D('Category')->get_ico($rs['card_type']);
			$new_list[] = $rs;
		}
		$data['list'] = $new_list ? $new_list : array();
		if($list){
			$this->success('获取数据成功！', $data);
		}else{
			$this->success('您尚未绑定银行卡！', $data);
		}
	}

	//提交并生成充值订单（WEB使用）
	public function submit_recharge(){
		$user_id   = $this->user_id();
		$pay_type  = $this->_post('pay_type','支付类型不能为空！');
		$amount    = $this->_post('amount','充值金额不能为空！');
		$order_sn  = generate_order_sn();

		if(is_numeric($amount) == false){
			$this->error('请输入正确金额！');
		}
		if(in_array($pay_type, array('alipay', 'weixin', 'upmp', 'kuaiqian', 'kuaiqian_bankcard')) == false){
			$this->error('您选择支付方式不支持！');
		}
		//处理成两位小数点，避免签名失败
		if($amount < 0.01){
			$this->error('最低充值金额为0.01');
		}

		//获取用户信息
		$where['user_id'] = $user_id;
		$info = D('User')->field('province_id,city_id')->where($where)->find();

		$data['province_id'] = $info['province_id'] ? $info['province_id'] : 0;
		$data['city_id']     = $info['city_id'] ? $info['city_id'] : 0;
		$data['order_sn']    = $order_sn;
		$data['user_id']     = $user_id;
		$data['pay_type']    = $pay_type;
		$data['amount']      = $amount;
		$data['give_amount'] = '0';
		$data['give_id']     = '0';
		$data['create_time'] = time();
		//终端类型
		$data['app_type']    = $this->app_type ? $this->app_type : 'system';

		if(D('RechargeOrder')->add($data)){
			$data['order_title']   = '充值金额：' . $amount . '元';
			$data['order_content'] = $data['order_title'];
			$this->success('生成充值订单成功！', $data);
		}else{
			$this->success('生成充值订单失败！', $data);
		}
	}

	//提现
	public function take_out(){
		$user_id = $this->user_id();
		$card_id = $this->_post('card_id', '请选择银行卡号');
		$money   = $this->_post('money', '提现金额不能为空！');

		if(is_numeric($money) == false || $money < 0){
			$this->error('对不起，您的金额有误！');
		}
		if($money < 50){
			$this->error('最低提现金额为50！');
		}
		// if($money % 50 != 0){
		// 	$this->error('提现金额必须为50的倍数！');
		// }
		//判断提现金额是否超过账户余额
		$usable_money = D('Account')->get_account_money($user_id, 'usable_account');
		if($usable_money < $money){
			$this->error('对不起，您要提现的金额超过账户余额！');
		}

		$where['card_id'] = $card_id;
		$card_info = D('UserBankCard')->where($where)->find();
		if(!$card_info['card_type']){
			$this->error('提现银行卡不存在！');
		}
		unset($where);

		$num  = 2;//提现次数
		$week = date('N');//星期中的第几天，数字
		// if(!in_array($week, array('6', '7'))){
		// 	$this->error('对不起，只能周六或周日才能提现！');
		// }
		// $week = 7;
		//开始时间
		$start_time = strtotime('-' . ($week - 1) . 'day');
		//结束时间
		$end_time   = strtotime('+' . (8 - $week) . 'day');

		$start_date = date('Y-m-d', $start_time);
		$end_date   = date('Y-m-d', $end_time);
		//转换成正常0点时间
		$start_time = strtotime($start_date);
		$end_time   = strtotime($end_date);

		//判断两周之前到现在的是否提现过
		$where['user_id']       = $user_id;
		$where['create_time'][] = array('egt', $start_time);
		$where['create_time'][] = array('lt', $end_time);
		$where['status']        = array('neq', 3);//提现失败的不算
		$count = D('UserTakeOut')->where($where)->count();
		//判断是否可以提现
		if($count >= $num){
			$this->error('对不起，您这一周内已有提现过' . $num . '次了！');
		}
		
		// 添加申请提现记录
		$data['user_id']     = $user_id;
		$data['card_id']     = $card_id;
		$data['bank_type']   = $card_info['card_type'] > 0 ? 1 : 2;
		$data['money']       = $money;
		$data['app_type']    = $this->app_type;
		$data['create_time'] = time();
		D('UserTakeOut')->add($data);
		$take_out_id = D('UserTakeOut')->getLastInsID();
		if($take_out_id > 0){
			// 扣除账户余额
			D('Account')->take_out($user_id, $money);
			$this->success('提现申请提交成功');
		}
		$this->error('提现申请失败，请联系管理员！');
	}
}